package com.zhangh.summer.admin.support.boot;


import com.zhangh.summer.admin.support.boot.common.ApplicationConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.context.ApplicationListener;

@Slf4j
public class ErpApplication {
    private SpringApplication application;

    public ErpApplication(){

    }

    public ErpApplication(Class cls){
        buildDefaultApplication(cls);
    }

    private void buildDefaultApplication(Class cls) {
        application = new SpringApplication(cls);
        application.setBannerMode(Banner.Mode.CONSOLE);
        application.setLogStartupInfo(false);
        application.setWebApplicationType(ApplicationConfig.config().getWebApplicationType());
    }

    public ErpApplication setWebApplicationType(WebApplicationType webApplicationType){
        application.setWebApplicationType(webApplicationType);
        ApplicationConfig.config().setWebApplicationType(webApplicationType);
        return this;
    }
    /**
     * 添加监听
     * @param listeners
     */
    public ErpApplication addListeners(ApplicationListener... listeners){
        application.addListeners(listeners);
        return this;
    }
    /**
     * 获取 SpringApplication
     * @return
     */
    public SpringApplication getSpringApplication(){
        return  application;
    }

    /**
     * 启动
     */
    public  void  run(String... args){
        application.run(args);
    }

    public static void  run(Class cls){
        new ErpApplication(cls).run();
    }

    public static void  run(Class cls,String[] args){
        new ErpApplication(cls).run(args);
    }
}

